package com.dz.syndata.service.impl;

import com.dz.syndata.bean.UpdateCarInfo;
import com.dz.syndata.bean.device.DeviceBean;
import com.dz.syndata.mapper.one.PrimaryDeviceMapper;
import com.dz.syndata.mapper.two.SecondaryUserMapper;
import com.dz.syndata.service.IDeviceInfoService;
import com.dz.syndata.service.impl.device.DeviceDataImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Service
public class DeviceInfoServiceImpl implements IDeviceInfoService {
    @Autowired
    PrimaryDeviceMapper primaryDeviceMapper;
    @Autowired
    SecondaryUserMapper  secondaryUserMapper;
    @Qualifier("redisR1Template")
    @Autowired
    RedisTemplate<String,Object>redisR1Template;
    @Qualifier("redisR2Template")
    @Autowired
    RedisTemplate<String,Object>redisR2Template;

    private List<String> lo;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void insertDeviceData() {
//        redisR2Template.opsForValue().set();
        List<String> list = null;
        List<DeviceBean> deviceBeans02=new ArrayList<>();
        List<DeviceBean> deviceBeans01=new ArrayList<>();
        List<DeviceBean> deviceBeans03=new ArrayList<>();
        List<DeviceBean> deviceBeans04=new ArrayList<>();
        List<DeviceBean> deviceBeans05=new ArrayList<>();
        try {
            List<DeviceBean> mDeviceDataLists = primaryDeviceMapper.findAll();
            list = DeviceDataImpl.getInstance().redExcel(null);
            for (int i = 0; i < mDeviceDataLists.size(); i++) {
                DeviceBean deviceBean = mDeviceDataLists.get(i);
                if (deviceBean.getCode().contains("HP1")) {
                    deviceBeans01.add(deviceBean);
                }
                if (deviceBean.getCode().contains("HP2")) {
                    deviceBeans02.add(deviceBean);
                }
                if (deviceBean.getCode().contains("HP3")) {
                    deviceBeans03.add(deviceBean);

                }
                if (deviceBean.getCode().contains("HP4")) {
                    deviceBeans04.add(deviceBean);
                }
                if (deviceBean.getCode().contains("HP5")) {
                    deviceBeans05.add(deviceBean);
                }
            }
            for (int i = 0; i < deviceBeans01.size(); i++) {
                DeviceBean deviceBean= deviceBeans01.get(i);
                String code = list.get(i);
                System.out.println("截取前：" + code);
                String endSubstring = code.substring(1, code.length() - 1);
                System.out.println("截取后：" + endSubstring);

                deviceBean.setTitle("1号机组" + endSubstring);
                primaryDeviceMapper.updateDeviceInfoTitle(deviceBean);
            }

            for (int i = 0; i < deviceBeans02.size(); i++) {
               DeviceBean deviceBean= deviceBeans02.get(i);
                String code = list.get(i);
                System.out.println("截取前：" + code);
                String endSubstring = code.substring(1, code.length() - 1);
                System.out.println("截取后：" + endSubstring);
                deviceBean.setTitle("2号机组" + endSubstring);
                primaryDeviceMapper.updateDeviceInfoTitle(deviceBean);
            }
            for (int i = 0; i < deviceBeans03.size(); i++) {
               DeviceBean deviceBean= deviceBeans03.get(i);

                String code = list.get(i);
                System.out.println("截取前：" + code);
                String endSubstring = code.substring(1, code.length() - 1);
                System.out.println("截取后：" + endSubstring);
                deviceBean.setTitle("3号机组" + endSubstring);
                primaryDeviceMapper.updateDeviceInfoTitle(deviceBean);
            }
            for (int i = 0; i < deviceBeans04.size(); i++) {
               DeviceBean deviceBean= deviceBeans04.get(i);

                String code = list.get(i);
                System.out.println("截取前：" + code);
                String endSubstring = code.substring(1, code.length() - 1);
                System.out.println("截取后：" + endSubstring);
                deviceBean.setTitle("4号机组" + endSubstring);
                primaryDeviceMapper.updateDeviceInfoTitle(deviceBean);
            }
            for (int i = 0; i < deviceBeans05.size(); i++) {
               DeviceBean deviceBean= deviceBeans05.get(i);
                String code = list.get(i);
                System.out.println("截取前：" + code);
                String endSubstring = code.substring(1, code.length() - 1);
                System.out.println("截取后：" + endSubstring);
                deviceBean.setTitle("5号机组" + endSubstring);
                primaryDeviceMapper.updateDeviceInfoTitle(deviceBean);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void insertDeviceCode() {
        List<String> list = null;
        try {
            list = DeviceDataImpl.getInstance().redExcel(null);
            for (int i = 0; i < list.size(); i++) {
                String code = list.get(i);
                System.out.println("截取前：" + code);
                String endSubstring = code.substring(1, code.length() - 1);
                System.out.println("截取后：" + endSubstring);
                primaryDeviceMapper.insertDeviceCode(endSubstring);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<DeviceBean> selectDeviceData() {
        return null;
    }

    @Override
    public void updateDeviceData() {
        ScheduledExecutorService execService = Executors.newScheduledThreadPool(1);
        execService.scheduleAtFixedRate(new Runnable() {
            public void run() {
                DeviceDataImpl deviceData = DeviceDataImpl.getInstance();
                List<DeviceBean> mDeviceList = deviceData.selectDevice(primaryDeviceMapper,redisR1Template,redisR2Template);
//                for (UpdateCarInfo mUpdateCarInfo : carInfos
//                ) {
//                    primaryDeviceMapper.updateCarInfo(mUpdateCarInfo);
//                }
                try {
                    for (int i = 0; i < 40; i++) {
                        Thread.sleep(1000L);
                    }
                    System.out.println("执行完毕^^^");
                    logger.info("deviceInfoServiceImpl->>>执行完毕");
                } catch (Exception e) {
                    logger.error(e.toString());
                    e.printStackTrace();

                }


            }
        }, 5, 2, TimeUnit.SECONDS);
    }

}
